/**
 * Created by caelan on 2018/10/26.
 * gis图形制作，请求行政区域
 */
var area = {
  /*行政区本地数据全国三级联动2016年7月*/
  resultData :null,
  init: function (areaCode) {
    var _this = this;
    /*获取区域行政数据*/
    $.ajax({
      // url: '../assets/localData/countryArea.json',
      url: './assets/localData/T_AREA.json',
      type: 'get',
      dataType: 'json',
      success: function (res) {
        if ( res ) {
            /*渲染dom*/
        _this.renderArea(res,areaCode)
        } else {
          console.log();
        }
      
      },
      error: function (e) {
        console.log(e);
      }
    });

     /*ui选择事件 省*/
    layForm.on('select(province)', function (data) {
        option = '';
        var areacode=data.value;
        // var cities = _this.resultData[data.value];
        for( let x = 0; x < _this.resultData.length; x++){
          let vt =_this.resultData[x];
          if(vt.areacode.length == 4 && vt.areacode.substring(0,2)==areacode){
            option += '<option value="'+ vt.areacode +'">'+ vt.areaname +'</option>'
          }
        }
        $('#gisGraphics').find('select[name="city"]').html(option);
        layForm.render(null, 'gisGraphics');
        $('select[name="cityArea"]').next().find('dd:eq(0)').trigger('click');
    });
    /*ui选择事件 市*/
    layForm.on('select(city)', function (data) {
      option = '';
      var areacode=data.value;
      // var cities = _this.resultData[data.value];
      for( let x = 0; x < _this.resultData.length; x++){
        let vt =_this.resultData[x];
        if(vt.areacode.length == 6 && vt.areacode.substring(0,4)==areacode){
          option += '<option value="'+ vt.areacode +'">'+ vt.areaname +'</option>'
        }
      }
      $('#gisGraphics').find('select[name="country"]').html(option);
      layForm.render(null, 'gisGraphics');
      $('select[name="cityArea"]').next().find('dd:eq(0)').trigger('click');
  });

  },
  //渲染数据
  renderArea: function (res,areaCode) {
	  // var areaCode = $("#addr-show02").attr("areacode");
	  var userinfo =null;
        if(localStorage.userInfo){
            userinfo =  JSON.parse(localStorage.userInfo);
        }
   var areaCode = userinfo.code;
    Global.currentAreaCode = areaCode;
    var _this = this;
    _this.resultData =  res.RECORDS;
    var prov = '';
    var city='';
    var country='';
    for ( let x = 0; x <  _this.resultData.length; x++ ) {
      var obj=_this.resultData[x];
      var areacodelen=obj.areacode.length;
   
      if(areacodelen == 2){  //省   
        prov += '<option value="'+ obj.areacode +'">'+  obj.areaname +'</option>';
      }else if(areacodelen==4){ //市
        city += '<option value="'+ obj.areacode +'">'+  obj.areaname +'</option>';
      }else{
        country += '<option value="'+ obj.areacode +'">'+  obj.areaname +'</option>';
      }
    }
    /*添加dom*/
    $('#gisGraphics').find('select[name="province"]').html(prov);
    $('#gisGraphics').find('select[name="city"]').html(city);
    $('#gisGraphics').find('select[name="country"]').html(country);
    /*ui组件渲染*/
    layForm.render(null, 'gisGraphics');
    /*触发省市县选择*/
    if(areaCode.length==2){
      $("#province").css({'pointer-events': "auto"});
      $("#city").css({'pointer-events': "auto"});
      $("#country").css({'pointer-events': "auto"});
      $('select[name="province"]').next().find('dd:eq(0)').trigger('click');
    } 
    if(areaCode.length==4){
      $("#province").css({'pointer-events': "none"});//禁止点击
      $("#city").css({'pointer-events': "auto"});
      $("#country").css({'pointer-events': "auto"});
      $('select[name="city"]').next().find('dd[lay-value="'+areaCode+'"]').trigger('click');
    }else if(areaCode.length==6){
      var city_areaCode=areaCode.substring(0,4);
      $("#province").css({'pointer-events': "none"});
      $("#city").css({'pointer-events': "none"});
      $('select[name="city"]').next().find('dd[lay-value="'+city_areaCode+'"]').trigger('click');
      $('select[name="country"]').next().find('dd[lay-value="'+areaCode+'"]').trigger('click');
    }
   
    
  }
};